package com.tedu.egweibodemo.controller;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.tedu.egweibodemo.base.response.JsonResult;
import com.tedu.egweibodemo.base.response.StatusCode;
import com.tedu.egweibodemo.mapper.WeiboMapper;
import com.tedu.egweibodemo.pojo.dto.WeiboAddParam;
import com.tedu.egweibodemo.pojo.entity.Weibo;
import com.tedu.egweibodemo.pojo.vo.UserVO;
import com.tedu.egweibodemo.pojo.vo.WeiboDetailVO;
import com.tedu.egweibodemo.pojo.vo.WeiboIndexVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpSession;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/v1/weibo")
@Tag(name =  "微博模块")
public class WeiboController {
    @Autowired
    private WeiboMapper weiboMapper;
    @PostMapping("/insert")
    @Operation(summary = "添加微博")
    @ApiOperationSupport(order = 10)
    public JsonResult insert(@RequestBody WeiboAddParam weiboAddParam, HttpSession session){
        UserVO userVO = (UserVO) session.getAttribute("user");
//        if (userVO == null) {
//            return new JsonResult(StatusCode.NOT_LOGIN);
//        }
        Weibo weibo = new Weibo();
        BeanUtils.copyProperties(weiboAddParam,weibo);
        weibo.setCreateTime(new Date());
        weibo.setUserId(userVO.getId());
        int num = weiboMapper.insert(weibo);
        return JsonResult.success();
    }
    @GetMapping("/list")
    @Operation(summary = "微博列表")
    @ApiOperationSupport(order = 20)
    public JsonResult list(){
        List<WeiboIndexVO> list = weiboMapper.select();
        return JsonResult.success(list);
    }
    @GetMapping("/detail")
    @Operation(summary = "查看微博")
    @ApiOperationSupport(order =30)
    public JsonResult weiboDetail(@Range(min =100,max=500,message = "微博的编号id必须在100-500之间")Long id){
        WeiboDetailVO weiboDetailVO = weiboMapper.selectDetail(id);
        return JsonResult.success(weiboDetailVO);
    }
}
